package com.lollipop.shardingjdbc.controller;

import com.lollipop.shardingjdbc.entity.ShardingUser;
import com.lollipop.shardingjdbc.mapper.ShardingUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: J.K
 * @Date: 2021-09-17 08:50
 * @Description:
 */
@RestController
public class ShardingUserController {

    @Autowired
    private ShardingUserMapper shardingUserMapper;

    /**
     * 查询所有
     *
     * @return
     */
    @RequestMapping("/userList")
    public List<ShardingUser> userList() {
        return shardingUserMapper.userList();
    }

    /**
     * 分页查询
     *
     * @return
     */
    @RequestMapping("/userListPage")
    public List<ShardingUser> userListPage() {
        return shardingUserMapper.userListPage();
    }

    /**
     * 排序
     *
     * @return
     */
    @RequestMapping("/userOrderBy")
    public List<ShardingUser> userOrderBy() {
        return shardingUserMapper.userOrderBy();
    }

    /**
     * 根据id查询用户信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/getByUserId")
    public List<ShardingUser> getByUserId(Long id) {
        return shardingUserMapper.getByUserId(id);
    }

    /**
     * 插入10条数据
     *
     * @return
     */
    @RequestMapping("/insertUser")
    public String insertUser() {
        for (int i = 1; i < 10; i++) {
            ShardingUser ShardingUser = new ShardingUser(i, "lollipop" + i, i);
            try {
                shardingUserMapper.insert(ShardingUser);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return "success";
    }
}
